package com.seaengine.proxy
{
	import com.seaengine.GameConfig;
	import com.seaengine.constData.Enums;
	import com.seaengine.core.event.GlobalEvent;
	import com.seaengine.core.event.GlobalEventDispatcher;
	import com.seaengine.resourcemanager.ResSystem;
	import com.seaengine.util.SmartURL;
	import com.seaengine.util.StringUtils;
	import com.seaengine.valueobject.BaseItemVo;
	import com.seaengine.valueobject.BattleResultVo;
	import com.seaengine.valueobject.EquipmentVo;
	import com.seaengine.valueobject.VoType;
	
	import flash.utils.Dictionary;
	import flash.utils.setTimeout;

	public class ItemProxy
	{
		
		public static const equipmentToType:Object = {
													  1:12 ,
													  2:12 ,
													  3:12 ,
													  4:12 ,
													  5:12 ,
													  6:12 ,
													  7:0 ,
													  8:1 ,
													  9:2 ,
													  12:3 ,
													  14:4 ,
													  10:5 ,
													  11:6 ,
													  13:7,
													  15:8,
													  15:9,
													  15:10,
													  15:11
															};
		
		public static const EQUIPMENT_BASE_INDEX:int = 20;
		
		private static var otherIndex:int = -1;
		
		private static var instance:ItemProxy;
		
		public var hasrequested:Boolean = false;
		public var requestIng:Boolean = false;
		
		
//		public var equipmentList:Array = [];
//		public var daojuList:Array = [];
//		public var baoshiList:Array = [];
//		public var taskList:Array = [];
		
		//目前最大36个
		public var equipmentList:Array = new Array(36);
		public var daojuList:Array = new Array(36);
		public var baoshiList:Array = new Array(36);
		public var taskList:Array = new Array(108);
		
		public var equipmentCount:uint;
		public var daoJuCount:uint;
		public var baoShiCount:uint;
		public var taskCount:uint;
		
		public var equipmentOpenCount:uint = 18;
		public var daoJuOpentCount:uint = 18;
		public var baoShiOpenCount:uint = 18;
		public var taskOpenCount:uint = 36;
		
		public var equipmentDic:Dictionary = new Dictionary(true);
		private var equipmentXmlDicById:Dictionary = new Dictionary(true);
		private var equipmentXmlDicByIndex:Dictionary = new Dictionary(true);
		
		public var daojuDic:Dictionary = new Dictionary(true);
		private var daojuXmlDicById:Dictionary = new Dictionary(true);
		private var daojuXmlDicByIndex:Dictionary = new Dictionary(true);
		
		public var baoshiDic:Dictionary = new Dictionary(true);
		private var baoshiXmlDicById:Dictionary = new Dictionary(true);
		private var baoshiXmlDicByIndex:Dictionary = new Dictionary(true);
		
		public var taskDic:Dictionary = new Dictionary(true);
		private var taskXmlDicById:Dictionary = new Dictionary(true);
		private var taskXmlDicByIndex:Dictionary = new Dictionary(true);
		
		private var tempIndex:int = -1;
		
		/**1穿装备 2脱装备 3交换装备*/
		public var equipmentAction:uint = 0;
		
		public var updateType:uint = 1;
		
		public function ItemProxy()
		{
			if (instance) return;
		}
		
		public static function getInstance():ItemProxy
		{
			if (!instance) instance = new ItemProxy();
			return instance;
		}
		
		public function getTempIndex():int
		{
			return tempIndex--;
		}
		
		public function init():void
		{
		}
		//登陆添加装备
		public function insertEquipment(_list:Array, update:Boolean = false):void
		{
			if (!_list) return;
			var len:int = _list.length;
			var vo:EquipmentVo;
			for (var i:int = 0; i < len; i++)
			{
				vo = new EquipmentVo();
				vo.index = _list[i]._index;
				vo.itemId = _list[i]._equipID;
				vo.count = 1;
				vo.category = VoType.EQUIPMENT;
				vo.quality = _list[i]._quality;
				
				vo.strengthLevel = _list[i]._strengthLevel;/**强化等级*/				
				vo.refiningProperties = _list[i]._refiningProperties;/**精炼*/
				vo.holes = _list[i]._holes;/**孔信息*/
				
				equipmentList[vo.index] = vo;
				if(vo.index >= 20)
				{
					equipmentCount++;
				}
				//updateTaskStatusByItemChange(vo);
//			    if(!update&&vo.quality>2)
//				{
//					GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.EFFECTS_SHOW,ToolBarCtrl.BEIBAO));
//				}
			}
			//装备数量都是为1的 所以不需要计算变化数量
			if(battleResultTemp)
			{
				battleResultTemp.items += _list.length;
				battleResultTemp.itemAry.push({ary:_list, type:VoType.EQUIPMENT});
			}
			if (update && getPackBackPanel()) 
			{
			//	getPackBackPanel().bagContent.selelctIndex = 0;
				//更新背包
			//	getPackBackPanel().switchTabbar();
			}
			if (getUserPanel()&& vo) {
				//更新人物面板
			//	_userPanel.updateOneEquipment(vo.index);
			}
		}
		
		public function insertEquipment2(_list:Array):Array
		{
			if(!_list)	return null;
			
			var result:Array = [];
			var equipmentVO:EquipmentVo = null;
			for each(var i:Object in _list)
			{
				equipmentVO = new EquipmentVo();
				equipmentVO.index = i._index;
				equipmentVO.itemId = i._equipID;
				equipmentVO.count = 1;
				equipmentVO.category = VoType.EQUIPMENT;
				equipmentVO.quality = i._quality;
				equipmentVO.strengthLevel = i._strengthLevel;
				equipmentVO.refiningProperties = i._refiningProperties;
				equipmentVO.holes = i._holes;
				result[equipmentVO.index] = equipmentVO;
			}
			return result;
		}
		
		private function updateTaskPackBackPanel():void
		{
			if (getPackBackPanel()) 
			{
//				getPackBackPanel().bagContent.selelctIndex = 1 //道具栏,看是否有镖旗
//				getPackBackPanel().switchTabbar();
//				getPackBackPanel().bagContent.selelctIndex = 0;
			}
			else
			{
	//			setTimeout(updateTaskPackBackPanel,1000);
			}
		}
				
		//添加(更新)任务道具
//		public function insertTesk(_list:Array, update:Boolean = false):void
//		{
//			if (!_list) return;
//			var len:int = _list.length;
//			var vo:TaskVo;
//			if(battleResultTemp)
//				battleResultTemp.items += _list.length;
//			if(battleResultTemp)
//			{
//				battleResultTemp.items=_list.length;
//				battleResultTemp.itemAry.push({ary:getAddedItemCount(_list,VoType.TASK), type:VoType.TASK});
//			}
//			for (var i:int = 0; i < len; i++)
//			{
//				vo = taskList[_list[i]._index];
//				if (vo) {
//					var intOldCount:int=vo.count;
//					vo.itemId = _list[i]._taskitemID
//					if(battleResultTemp)
//						vo.count = _list[i]._lapCnt+intOldCount;
//					else
//						vo.count = _list[i]._lapCnt;
//						
//					
//					
//				} else {
//					vo = new TaskVo();
//					vo.index = _list[i]._index;
//					vo.itemId = _list[i]._taskitemID;
//					vo.count = _list[i]._lapCnt;
//					vo.taskId = _list[i]._taskID;
//					taskCount += 1;
//				}
//				taskList[vo.index] = vo;
//				if(vo.trackDisplay==1)
//					PlayerAttribute.getInstance().trackDisplay=true;
//				if (vo.count == 0) {
//					taskCount -= 1;
//					if(vo.trackDisplay==1)
//						PlayerAttribute.getInstance().trackDisplay=false;
//					taskList[vo.index] = null;
//					delete taskList[vo.index];
//				}
//				updateTaskStatusByItemChange(vo);
//			}
//			
//			if (update && getPackBackPanel()) 
//			{
//				getPackBackPanel().bagContent.selelctIndex = 3;
//				//更新背包
//				getPackBackPanel().switchTabbar();
//			}
//		}
		
		/**
		 * 插入道具
		 */		
		public function insertItem(arr:Array,isUpdate:Boolean = false):void
		{
			var vo:BaseItemVo;
			if(battleResultTemp)
			{
				battleResultTemp.items=arr.length;
				battleResultTemp.itemAry.push({ary:getAddedItemCount(arr,VoType.DAOJU), type:VoType.DAOJU});
			}
			for each(var obj:Object in arr)
			{
				if(daojuList[obj["_index"]] == null)
				{
					vo = new BaseItemVo();
					vo.itemId = obj["_itemID"];
					vo.index = obj["_index"];
					vo.count = obj["_lapCnt"];
					daojuList[vo.index] = vo;
					daoJuCount += 1;
				}
				else
				{
					vo = daojuList[obj["_index"]];
					vo.itemId = obj["_itemID"];
					if(battleResultTemp)
						vo.count = obj["_lapCnt"] + vo.count;
					else
						vo.count = obj["_lapCnt"];
				}
				if (vo.count == 0) {
					daoJuCount -= 1;
					if(vo.trackDisplay==1)
				//		PlayerAttribute.getInstance().trackDisplay=false;
					daojuList[vo.index] = null;
					delete daojuList[vo.index];
				}
				else
				{
					ItemProxy.getInstance().getBaseData(VoType.DAOJU, vo.itemId, vo.index, vo, null, false);
				}
			//	updateTaskStatusByItemChange(vo);
			}
			if (isUpdate && getPackBackPanel() != null) 
			{
			//	getPackBackPanel().bagContent.selelctIndex = 1;
				//更新背包
			//	getPackBackPanel().switchTabbar();
			}
		}
		/**
		 * 插入宝石
		 */		
		public function insertBaoShi(arr:Array,isUpdate:Boolean = false):void
		{
			var vo:BaseItemVo;
			if(battleResultTemp)
			{
				battleResultTemp.items=arr.length;
				battleResultTemp.itemAry.push({ary:getAddedItemCount(arr,VoType.BAOSHI), type:VoType.BAOSHI});
			}
			for each(var obj:Object in arr)
			{
				if(baoshiList[obj["_index"]] == null)
				{
					vo = new BaseItemVo();
					vo.itemId = obj["_itemID"];
					vo.index = obj["_index"];
					vo.count = obj["_lapCnt"];
					baoshiList[vo.index] = vo;
					baoShiCount += 1;
				}
				else
				{
					vo = baoshiList[obj["_index"]];
					vo.itemId = obj["_itemID"];
					if(battleResultTemp)
						vo.count = obj["_lapCnt"] + vo.count;
					else
						vo.count = obj["_lapCnt"] ;
				}
				if(vo.trackDisplay==1)
				//	PlayerAttribute.getInstance().trackDisplay=true;
				if (vo.count == 0) {
					baoShiCount -= 1;
					if(vo.trackDisplay==1)
					//	PlayerAttribute.getInstance().trackDisplay=false;
					baoshiList[vo.index] = null;
					delete baoshiList[vo.index];
				}
				//updateTaskStatusByItemChange(vo);
			}
			if (isUpdate && getPackBackPanel() != null) 
			{
				//getPackBackPanel().bagContent.selelctIndex = 2;
				//更新背包
				//getPackBackPanel().switchTabbar();
			}
		}
		
		/**
		 * 通过下标来获得背包道具数据对象
		 * @param index 下标
		 */		
		public function getDaoJuVO(index:uint):BaseItemVo
		{
			return daojuList[index];
		}
		
		/**
		 * 通过下标来获得背包装备数据对象
		 * @param index 下标
		 */		
		public function getEquipmentVO(index:uint):BaseItemVo
		{
			return equipmentList[index];
		}
		
		//得到道具的真实数量,现在服务器下发的是最终值
		public function getAddedItemCount(aryNewDaoju:Array,intType:int):Array
		{
			var dicTemp:Array
			switch (intType)
			{
				case VoType.EQUIPMENT:
					dicTemp = equipmentList;
					break;
				case VoType.TASK:
					dicTemp = taskList;
					break;
				case VoType.DAOJU:
					dicTemp = daojuList;
					break
				case VoType.BAOSHI:
					dicTemp = baoshiList;
					break
				default:
					dicTemp = daojuList;
					break
			}
			var list:Array = new Array();
			for each(var obj:Object in aryNewDaoju)
			{
				var vo:BaseItemVo = dicTemp[obj["_index"]] as BaseItemVo;
				if(vo!=null)
				{
					if(intType==VoType.TASK && vo.itemId==obj["_taskitemID"])
					{
						obj["_lapCnt"]=obj["_lapCnt"]-vo.count
					}
					else if(vo.itemId==obj["_itemID"])
					{
						obj["_lapCnt"]=obj["_lapCnt"]-vo.count
					}
				}
				list.push(obj);
			}
			return list;
		}
		
		//删除
		public function deleteItem(ary:Array, type:int):void
		{
			var list:Array = getListByType(type);
			updateType = type;
			switch(type)
			{
				case VoType.EQUIPMENT:
					GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.DISCARD_EQUIP));
					break;
				case VoType.BAOSHI:
					GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.DISCARD_GEM));
					break;
				case VoType.TASK:
					GlobalEventDispatcher.getInstance().dispatchEvent(new GlobalEvent(GlobalEvent.DISCARD_TASK));
					break;
			}
			for (var i:int = 0; i < ary.length; i++) {
				var vo:BaseItemVo = list[ary[i]._index] as BaseItemVo;
				if(vo.trackDisplay==1)
						//PlayerAttribute.getInstance().trackDisplay=false;
				list[ary[i]._index] = null;
				//updateTaskStatusByItemChange(ary[i]);
			}
			if (getPackBackPanel()) 
			{
				//getPackBackPanel().bagContent.selelctIndex = type -1;
				//更新背包
				//getPackBackPanel().switchTabbar();
			}
		}
		
		public function clearCurrentIndex(type:uint):void
		{
			var list:Array = getListByType(type + 1);
			list.length = 0;
		}
		
		/**
		 * 使用道具
		 */		
		public function useDaoJu(index:uint):void
		{
			var vo:BaseItemVo;
			vo = daojuList[index] as BaseItemVo;
			vo.count--;
			if(vo.count == 0)
			{
				ItemProxy.getInstance().throwOutItem(vo.category,index);
			}
			else if(getPackBackPanel() != null)
			{
				//getPackBackPanel().bagContent.selelctIndex = 1;
				//getPackBackPanel().switchTabbar();
			}
		}
		
		private var battleResultTemp:BattleResultVo;
		public function settleResultItemChange(ary:Array,battleResultVo:BattleResultVo=null):BattleResultVo
		{
			battleResultTemp=battleResultVo;
			var temp:Array;
			var type:int;
			var action:int;
			var index:uint;
			for (var i:int = 0; i < ary.length; i++)
			{
				temp = ary[i].ary;
				type = ary[i].type;
				action = ary[i].action;
				index = ary[i].index;
				switch(type)
				{
					case VoType.EQUIPMENT:
						if (action == 1) insertEquipment(temp, true);
						else deleteItem(temp, type);
						break;
					case VoType.DAOJU:
						if (action == 1)
						{
							insertItem(temp, true);
						}
						else 
						{
							useDaoJu(index);
						}
						break;
					case VoType.BAOSHI:
						if (action == 1) 
						{
							insertBaoShi(temp, true);
						}
						else 
						{
							deleteItem(temp, type);
						}
						break;
					case VoType.TASK:
						//if (action == 1) 
							//insertTesk(temp, true);
						//else 
							deleteItem(temp, type);
						break;
				}
			}
//			if (getPackBackPanel()) {
//				//更新背包
//				getPackBackPanel().tabBar.selectedIndex = type;
//				getPackBackPanel().switchTabbar();
//			}
			return battleResultTemp;
		}
		
		public function getList(type:int):Array
		{
			switch(type)
			{
				case VoType.EQUIPMENT:
					return equipmentList;
					break;
				case VoType.DAOJU:
					return daojuList;
					break;
				case VoType.BAOSHI:
					return baoshiList;
					break;
				case VoType.TASK:
					return taskList;
					break;
			}
			return null;
		}
		//得到身上装备和背包的方法.得到其他类型的道具请用getBaseItem
		public function getBaseData(type:int, id:int, index:int, vo:BaseItemVo, callBack:Function, getIcon:Boolean):void
		{
			//if (callBack == null) return;
			var obj:Object;
			switch(type)
			{
				case VoType.EQUIPMENT:
					if (equipmentDic[id]) 
					{
						//vo.tooltips = ToolTipParse.toolTipsParse(equipmentDic[id], vo);
						//vo.descrip = ToolTipParse.toolTipsParse1(equipmentDic[id]);
						if (vo.index < equipmentList.length) 
						{
							vo.category = VoType.EQUIPMENT;
							setEquipmentData(vo, equipmentDic[id]);
						}
						if (callBack != null)
							callBack(id, getIcon);
					} 
					else 
					{
						obj = {id:id, index:index, callBack:callBack, vo:vo, getIcon:getIcon};
						equipmentXmlDicByIndex[index] = obj;
						equipmentXmlDicById[id] = obj;
						ResSystem.getInstance().getRes(GameConfig.m_ResConfig + "item/xml/itemType_" + id + ".xml", onEquipment, index);
					}
					break;
				case VoType.DAOJU://道具
					if(daojuDic[id] != null)
					{
						//vo.tooltips = ToolTipParse.toolTipsParse(daojuDic[id]);
						//vo.descrip = ToolTipParse.toolTipsParse1(daojuDic[id]);
						if (vo.index < daojuList.length) 
						{
							vo.category = VoType.DAOJU;
							setEquipmentData(vo, daojuDic[id]);
						}
						if (callBack != null)
							callBack(id, getIcon);
					}
					else
					{
						obj = {id:id, index:index, callBack:callBack, vo:vo, getIcon:getIcon};
						daojuXmlDicByIndex[index] = obj;
						daojuXmlDicById[id] = obj;
						ResSystem.getInstance().getRes(GameConfig.m_ResConfig + "item/xml/itemType_" + id + ".xml", onDaoJu, index);
					}
					break;
				case VoType.BAOSHI://宝石
					if(baoshiDic[id] != null)
					{
						//vo.tooltips = ToolTipParse.toolTipsParse(baoshiDic[id]);
						//vo.descrip = ToolTipParse.toolTipsParse1(baoshiList[id]);
						if (vo.index < daojuList.length) 
						{
							vo.category = VoType.BAOSHI;
							setEquipmentData(vo, baoshiDic[id]);
						}
						if (callBack != null)
							callBack(id, getIcon);
					}
					else
					{
						obj = {id:id, index:index, callBack:callBack, vo:vo, getIcon:getIcon};
						baoshiXmlDicByIndex[index] = obj;
						baoshiXmlDicById[id] = obj;
						ResSystem.getInstance().getRes(GameConfig.m_ResConfig + "item/xml/itemType_" + id + ".xml", onBaoshi, index);
					}
					break;
				case VoType.TASK:
					if (taskDic[id]) 
					{
						//vo.tooltips = ToolTipParse.toolTipsParse(taskDic[id]);
						//vo.descrip = ToolTipParse.toolTipsParse1(taskDic[id]);
						if (vo.index < taskList.length) 
						{
							vo.category = VoType.TASK;
							setTaskData(vo, taskDic[id]);
						}
						if (callBack != null)
							callBack(id, getIcon);
					} 
					else 
					{
						obj = {id:id, index:index, callBack:callBack, vo:vo, getIcon:getIcon};
						taskXmlDicByIndex[index] = obj;
						taskXmlDicById[id] = obj;
						ResSystem.getInstance().getRes(GameConfig.m_ResConfig + "item/xml/itemType_" + id + ".xml", onTask, index);
					}
					break;
			}
		}
		private var getBaseItemCallBackList:Dictionary=new Dictionary();
		private var getBaseItemCountList:Dictionary=new Dictionary();
		public function getBaseItem(id:int, callBack:Function,count:int=1):void
		{
			if (callBack == null) return;
			var filename:String=GameConfig.m_ResConfig + "item/xml/itemType_" + id + ".xml";
			//ResSystem 返回filename的格式有要求
			var _parsedURL:SmartURL = new SmartURL(filename);
			var _fileName:String = _parsedURL.tofile();
			if( getBaseItemCallBackList[_fileName] == null )
			{	
				getBaseItemCallBackList[_fileName] = new Array();
				getBaseItemCountList[_fileName] = new Array();
			}
			if(callBack!=null)
			{
				(getBaseItemCallBackList[_fileName] as Array).push(callBack);
				(getBaseItemCountList[_fileName] as Array).push(count);
			}
			ResSystem.getInstance().getRes(filename, onGetBaseItem);
		}
		private function onGetBaseItem(_fileName:String, obj:Object):void
		{
			var tempXml:XML = new XML(obj.item[0]);
			var tempBaseItemVo:BaseItemVo=new BaseItemVo();
			tempBaseItemVo.itemId=tempXml.id;
			tempBaseItemVo.name=tempXml.id.@name;
			tempBaseItemVo.quality=tempXml.id.@quality;
			tempBaseItemVo.category = tempXml.id.@category;
			//tempBaseItemVo.tooltips = ToolTipParse.toolTipsParse( new XML(obj), tempBaseItemVo);
			//tempBaseItemVo.descrip = ToolTipParse.toolTipsParse1( new XML(obj));
			tempBaseItemVo.icon=tempXml.id.@icon;
			
			
			var _CallbackFns:Array = getBaseItemCallBackList[_fileName] as Array;
			var _callBackObj:Array = getBaseItemCountList[_fileName] as Array; 
			if(_CallbackFns== null)
				return;
			for(var i:Number = 0 ; i < _CallbackFns.length;i++)
			{	
				if(_callBackObj[i] != null)	
					tempBaseItemVo.count=_callBackObj[i];
				else
					tempBaseItemVo.count=1;
				_CallbackFns[i](tempBaseItemVo.itemId,tempBaseItemVo);
			}
			getBaseItemCallBackList[_fileName] = null;
			delete getBaseItemCallBackList[_fileName];
			getBaseItemCountList[_fileName] = null;
			delete getBaseItemCountList[_fileName];
			setTimeout(function ():void{ResSystem.getInstance().unloadRes(_fileName);},1000);
		}
		private function onEquipment(str:String, obj:Object, index:int):void
		{
			var id:int = Number(StringUtils.extractMainNameFromFileName(str).split("_")[1]);
			equipmentDic[id] = new XML(obj);
			try
			{
				var vo:BaseItemVo = equipmentXmlDicByIndex[index].vo;
				//vo.tooltips = ToolTipParse.toolTipsParse(equipmentDic[id], vo);
				//vo.descrip = ToolTipParse.toolTipsParse1(equipmentDic[id]);
				vo.category = VoType.EQUIPMENT;
				if (vo.index < equipmentList.length) {
					setEquipmentData(vo, equipmentDic[id]);
				}
				//trace("equipmentXmlDicByIndex[index]:"+index)
				equipmentXmlDicByIndex[index].callBack(id, equipmentXmlDicByIndex[index].getIcon);
				
				equipmentXmlDicByIndex[index] = null;
				delete equipmentXmlDicByIndex[index];
				
				equipmentXmlDicById[id] = null;
				delete equipmentXmlDicById[id];
			}
			catch(e:Error)
			{
				//trace("出错了");
			}
		}
		
		private function onDaoJu(str:String, obj:Object, index:int):void
		{
			var id:int = Number(StringUtils.extractMainNameFromFileName(str).split("_")[1]);
			daojuDic[id] = new XML(obj);
			try
			{
				var vo:BaseItemVo = daojuXmlDicByIndex[index].vo;
				//vo.tooltips = ToolTipParse.toolTipsParse(daojuDic[id]);
				//vo.descrip = ToolTipParse.toolTipsParse1(daojuDic[id]);
				vo.category = VoType.DAOJU;
				if (vo.index < daojuList.length) 
				{
					setEquipmentData(vo, daojuDic[id]);//和装备一样
				}
				daojuXmlDicByIndex[index].callBack(id, daojuXmlDicByIndex[index].getIcon);
				
				daojuXmlDicByIndex[vo.index] = null;
				delete daojuXmlDicByIndex[vo.index];
				
				daojuXmlDicById[id] = null;
				delete daojuXmlDicById[id];
			}
			catch(e:Error)
			{
				//trace("出错了");
			}
		}
		
		private function onBaoshi(str:String, obj:Object, index:int):void
		{
			var id:int = Number(StringUtils.extractMainNameFromFileName(str).split("_")[1]);
			baoshiDic[id] = new XML(obj);
			try
			{
				var vo:BaseItemVo = baoshiXmlDicByIndex[index].vo;
				//vo.tooltips = ToolTipParse.toolTipsParse(baoshiDic[id]);
				//vo.descrip = ToolTipParse.toolTipsParse1(baoshiDic[id]);
				vo.category = VoType.BAOSHI;
				if (vo.index < baoshiList.length) 
				{
					setEquipmentData(vo, baoshiDic[id]);//和装备一样
				}
				baoshiXmlDicByIndex[index].callBack(id, baoshiXmlDicByIndex[index].getIcon);
				
				baoshiXmlDicByIndex[vo.index] = null;
				delete baoshiXmlDicByIndex[vo.index];
				
				baoshiXmlDicById[id] = null;
				delete baoshiXmlDicById[id];
			}
			catch(e:Error)
			{
				//trace("出错了");
			}
		}
		
		private function setEquipmentData(vo:BaseItemVo, xml:XML):void
		{
			xml = xml.item[0];
			vo.itemId = xml.id;
			vo.type = xml.id.@sort;
			vo.name = xml.id.@name;
			vo.capacity = xml.id.@capacity;
			if (xml.id.@brief) vo.descrip = xml.id.@brief;
			if (xml.id.@icon) vo.icon = xml.id.@icon;
			if (xml.id.@quality) vo.quality = xml.id.@quality;
			if (xml.id.@priceSell) vo.sellPrice = xml.id.@priceSell;
			if (xml.id.@priceBuy) vo.buyPrice = xml.id.@priceBuy;
			if (xml.id.@unbindable) vo.bindState = xml.id.@unbindable;
			if (xml.id.@expireType) vo.expiredType = xml.id.@expireType;
			if (xml.id.@expireTime) vo.expiration = xml.id.@expireTime;
			if (xml.id.@itemLevel) vo.itemLevel = xml.id.@itemLevel;
			if (xml.id.@propType != null)
			{
				for each(var obj:Object in xml.enhance)
				{
					var propType:uint = obj.@propType;
					var propValue:uint = obj.@value;
					if(propType == Enums.PROPERTY_PARTY)
					{
						vo.party = propValue;
					}
					vo.propType = propType;
				}
				for each(var obj1:Object in xml.cond)
				{
					var propType1:uint = obj1.@condPropType;
					var propValue1:uint = obj1.@condValue;
					vo.propType = propType1;
					if(propType1 == Enums.PROPERTY_LEVEL)
					{
						vo.useLevel = propValue1;
					}
					if(propType1 == Enums.PROPERTY_PARTY)
					{
						vo.party = propValue1;
					}
				}
			}
			if(xml.id.@attribs && xml.id.@attribs & Enums.UPROPERTY_TRACKDISPLAY)
			{
				if(vo.index>-1)
					//PlayerAttribute.getInstance().trackDisplay = true;
				vo.trackDisplay = 1;
			}
			else
				vo.trackDisplay = 0;
			
			if (xml.id.@attribs) vo.attribs = xml.id.@attribs;
			if (xml.id.@enhancementRule) vo.enhancementRule = xml.id.@enhancementRule;
			if (xml.id.@refiningRule) vo.refiningRule = xml.id.@refiningRule;
			if (xml.id.@refining) vo.refining = xml.id.@refining;
			if (xml.id.@gemHoles) vo.gemHoles = xml.id.@gemHoles;
			if(xml.compositions != undefined)
			{
				vo.compositions = [];
				for each(var compositions:XML in xml.compositions)
					vo.compositions.push(int(compositions.@v));
			}
				
			var len:int = xml.basic.length();
			var i:int;
			for (i = 1; i <= len; i++)
			{
				vo["basicAtt"+ i +"_id"] = xml.basic[i - 1].@basicPropType;
				vo["basicAtt"+ i] = xml.basic[i - 1].@basicValue;
			}
			len = xml.enhance.length();
			for (i = 1; i <= len; i++)
			{
				vo["att"+ i +"_id"] = xml.enhance[i - 1].@propType;
				vo["att"+ i] = xml.enhance[i - 1].@value;
			}
		}
		
		private function onTask(str:String, obj:Object, index:int):void
		{
			var id:int = Number(StringUtils.extractMainNameFromFileName(str).split("_")[1]);
			taskDic[id] = new XML(obj);
			try
			{
				var vo:BaseItemVo = taskXmlDicByIndex[index].vo;
				//vo.tooltips = ToolTipParse.toolTipsParse(taskDic[id]);
//				vo.tooltips = newTaskToolTips(taskDic[id]);
				vo.category = VoType.TASK;
				if (vo.index < taskList.length) {
					setTaskData(vo, taskDic[id]);
				}
				taskXmlDicByIndex[index].callBack(id, taskXmlDicByIndex[index].getIcon);
				
				taskXmlDicByIndex[vo.index] = null;
				delete taskXmlDicByIndex[vo.index];
				
				taskXmlDicById[id] = null;
				delete taskXmlDicById[id];
			}
			catch(e:Error)
			{
				//trace("出错了");
			}
		}
		
		private function setTaskData(vo:BaseItemVo, xml:XML):void
		{
			xml = xml.item[0];
			vo.itemId = xml.id;
			vo.type = xml.id.@category;
			vo.name = xml.id.@name;
			vo.capacity = xml.id.@capacity;
			if (xml.id.@brief) vo.descrip = xml.id.@brief;
			if (xml.id.@icon) vo.icon = xml.id.@icon;
			if (xml.id.@quality) vo.quality = xml.id.@quality;
			if (xml.id.@priceSell) vo.sellPrice = xml.id.@priceSell;
			if (xml.id.@priceBuy) vo.buyPrice = xml.id.@priceBuy;
			if (xml.id.@unbindable) vo.bindState = xml.id.@unbindable;
			if (xml.id.@expireType) vo.expiredType = xml.id.@expireType;
			if (xml.id.@expireTime) vo.expiration = xml.id.@expireTime;
			if(xml.id.@attribs && xml.id.@attribs & Enums.UPROPERTY_TRACKDISPLAY)
			{
				//PlayerAttribute.getInstance().trackDisplay = true;
				vo.trackDisplay = 1;
			}
			else
				vo.trackDisplay = 0;
				
			var len:int = xml.basic.length();
			var i:int;
			for (i = 1; i <= len; i++)
			{
				vo["basicAtt"+ i +"_id"] = xml.basic[i - 1].@basicPropType;
				vo["basicAtt"+ i] = xml.basic[i - 1].@basicValue;
			}
			len = xml.enhance.length();
			for (i = 1; i <= len; i++)
			{
				vo["att"+ i +"_id"] = xml.enhance[i - 1].@propType;
				vo["att"+ i] = xml.enhance[i - 1].@value;
			}
		}
		
		public function updateEquipmentToolTips():void
		{
			var len:int = equipmentList.length;
			var vo:EquipmentVo;
			for (var i:int = 0; i < len; i++) {
				vo = equipmentList[i];
				if (vo && equipmentDic[vo.itemId]) {
					//vo.tooltips = ToolTipParse.toolTipsParse(equipmentDic[vo.itemId], vo);
					//vo.descrip = ToolTipParse.toolTipsParse1(equipmentDic[vo.itemId]);
				}
			}
		}
		
		public function getPackBackPanel():Object
		{
//			if (_backpack) return _backpack;
//			else {
//				_backpack = PanelManager.getInstance().getPanel(PanelType.BACKPACK_PANEL) as BackPackPanel;
//				return _backpack;
//			}
			return null
		}
		
		public function getUserPanel():Object
		{
//			if (_userPanel) return _userPanel;
//			else {
//				_userPanel = PanelManager.getInstance().getPanel(PanelType.USER_PANEL) as UserPanel;
//				return _userPanel;
//			}
			return null
		}
		
		public function exChangeItem(_type:int, _from:int, _to:int, nei:Boolean = false):void
		{
			if (!nei) 
			{
				//networkLogicProtocol.getInstance().requestExchangeEquip(_from, _to);
			}
			else 
			{
				//networkLogicProtocol.getInstance().requestExchangeBagPosition(_type, _from, _to);
			}
		}
		
		public function throwOutItem(_type:int, index:int):void
		{
			var list:Array;
			switch(_type)
			{
				case VoType.EQUIPMENT:
					list = equipmentList;
					break;
				case VoType.DAOJU:
					list = daojuList;
					break;
				case VoType.BAOSHI:
					list = baoshiList;
					break;
			}
			//updateTaskStatusByItemChange(list[index]);
			//是否扔掉了镖旗
			var vo:BaseItemVo = list[index] as BaseItemVo;
			if(vo != null)
			{
				if(vo.trackDisplay==1)
				{
					//PlayerAttribute.getInstance().trackDisplay=false;
				}
				list[index] = null;
			}
			//if (getPackBackPanel()) getPackBackPanel().reFreshOne(index, _type);
		}
		
		public function onResponse(obj:Object):void
		{
			equipmentAction = 0;
			if(getEquipmentVO(obj._to) != null)
			{
				equipmentAction = 3;
			}
			var list:Array = getListByType(obj._type);
			var oldFrom:BaseItemVo = list[obj._from];//原
			var oldTo:BaseItemVo = list[obj._to];//新
			list[obj._from] = null;
			list[obj._to] = null;
			list[obj._from] = oldTo;
			if (oldTo)
			{
				oldTo.index = obj._from;
			}
			list[obj._to] = oldFrom;
			if (oldFrom)
			{
				oldFrom.index = obj._to;
			}
			
			if (getPackBackPanel() || getUserPanel()) 
			{
				if (obj._from < 20 && obj._type == VoType.EQUIPMENT) 
				{
					if (getUserPanel())//脱装备
					{
						equipmentAction = 2;
						//getUserPanel().updateOneEquipment(obj._from);
					}
				} 
				else 
				{
					if (getPackBackPanel())
					{
						//getPackBackPanel().reFreshOne(obj._from, obj._type);	
					}
				}
				if (obj._to < 20 && obj._type == VoType.EQUIPMENT) 
				{
					if (getUserPanel())//穿装备
					{
						if(equipmentAction != 3)
						{
							equipmentAction = 1;
						}
						//getUserPanel().updateOneEquipment(obj._to);
					}
				} 
				else 
				{
					if (getPackBackPanel())
					{
						//getPackBackPanel().reFreshOne(obj._to, obj._type);
					}
				}
			}
		}
		
		public function exchangeEquip():void
		{
			if(equipmentAction == 1)
			{
				equipmentCount--;
			}
			else if(equipmentAction == 2)
			{
				equipmentCount++;
			}
//			if (getPackBackPanel())
//			{
//				if(getPackBackPanel().bagContent.selelctIndex != 0)
//				{
//					getPackBackPanel().bagContent.selelctIndex = 0;
//					getPackBackPanel().switchTabbar();
//				}
//				getPackBackPanel().bagContent.updateBackGrid();
//			}
		}
		
		private function getListByType(type:int):Array
		{
			switch(type)
			{
				case VoType.EQUIPMENT:
					return equipmentList;
					break;
				case VoType.DAOJU:
					return daojuList;
					break;
				case VoType.BAOSHI:
					return baoshiList;
					break;
				case VoType.TASK:
					return taskList;
					break;
			}
			return null;
		}
		
		public function findEmptyIndex(type:int, begin:int):int
		{
			var index:int = -1;
			var check:Array;
			switch(type) {
				case VoType.EQUIPMENT:
					check = equipmentList;
					break;
			}
			for (var i:int = begin; i < check.length + 1; i++)
			{
				if (!check[i]) {
					index = i;
					break;
				}
			}
			return index;
		}
		
		public function getEmptyIndex():int
		{
			return otherIndex--;
		}
		
//////////////////////////////////////////////////////压力测试//////////////////////////////////////////////////////////////////////////////
		public function randomExChange():void
		{
			var random:int = Math.ceil(Math.random() * 16);
			switch(random)
			{
				case 1:
					//随机
//					networkLogicProtocol.getInstance().requestExchangeElement(1, Math.ceil(Math.random() * 59), Math.ceil(Math.random() * 59));
					break;
				case 2:
				case 3:
				case 4:
				case 5:
				case 6:
					//转移
					var begin:int = getHasBoxPos();
					var temp:int = findEmptyIndex(VoType.EQUIPMENT, 20);
					if (begin != -1 && temp != -1) {
//						networkLogicProtocol.getInstance().requestExchangeElement(1, begin, temp);
					}
					break;
				case 7:
				case 8:
				case 9:
				case 10:
				case 11:
					//卸下
					var begin1:int = getUserPos();
					var temp1:int = findEmptyIndex(VoType.EQUIPMENT, 20);
					if (begin1 != -1 && temp1 != -1) {
//						networkLogicProtocol.getInstance().requestExchangeElement(1, begin1, temp1);
					}
				case 12:
				case 13:
				case 14:
				case 15:
				case 16:
					//穿上
					var randomPos:int = getRandomPos();
					if (randomPos != -1) {
						var vo:EquipmentVo = equipmentList[randomPos];
						var needList:Array = [8, 9, 10, 12, 15, 14, 13, 16];
						var index:int = needList.indexOf(vo.type);
						if (index > -1) {
//							networkLogicProtocol.getInstance().requestExchangeElement(1, randomPos, index);
						}
					}
					break;
			}
		}
		
		private function getHasBoxPos():int
		{
			var temp:int = -1;
			for (var i:int = 20; i < 60; i++) {
				if (equipmentList[i]) {
					temp = i;
					break;
				}
			}
			return temp;
		}
		
		private function getUserPos():int
		{
			var temp:int = -1;
			for (var i:int = 0; i < 20; i++) {
				if (equipmentList[i]) {
					temp = i;
					break;
				}
			}
			return temp;
		}
		
		private function getRandomPos():int
		{
			var return_pos:int = -1;
			var list:Array = [];
			for (var i:int = 20; i < 60; i++) {
				if (equipmentList[i]) {
					list.push(i);
				}
			}
			if (list.length > 0) {
				return_pos = list[Math.floor(list.length * Math.random())];
			}
			return return_pos;
		}
		
		/**
		 * 无意义的
		 */		
		private function insignificance(id:int,getIcon:Boolean):void
		{
			//无意义的但是不能删除，为了回掉方法
		}
		
		/**
		 * 背包是否满
		 * @return true满 false没满 
		 */		
		public function isBagFull(type:uint):Boolean
		{
			var result:Boolean = false;
			switch(type)
			{
				case Enums.ITEM_CATEGORY_EQUIPMENT:
					if(equipmentCount >= equipmentOpenCount && equipmentOpenCount != 0)
					{
						//Alert.show("装备背包已满");
						result = true;
					}
					break;
				case Enums.ITEM_CATEGORY_ITEM:
					if(daoJuCount >= daoJuOpentCount && daoJuOpentCount != 0)
					{
						//Alert.show("道具背包已满");
						result = true;
					}
					break;
				case Enums.ITEM_CATEGORY_GEM:
					if(baoShiCount >= baoShiOpenCount && baoShiOpenCount != 0)
					{
						//Alert.show("宝石背包已满");
						result = true;
					}
					break;
				case Enums.ITEM_CATEGORY_TASKITEM:
					if(taskCount >= taskOpenCount && taskOpenCount != 0)
					{
						//Alert.show("任务背包已满");
						result = true;
					}
					break;
			}
			return result;
		}
		
		/**
		 * 背包是否满
		 * @return true满 false没满 
		 */		
		public function isAllBagFull():Boolean
		{
			var result:Boolean = false;
			if(equipmentCount >= equipmentOpenCount && equipmentOpenCount != 0)
			{
				//Alert.show("装备背包已满");
				return result = true;
			}
			if(daoJuCount >= daoJuOpentCount && daoJuOpentCount != 0)
			{
				//Alert.show("道具背包已满");
				return result = true;
			}
			if(baoShiCount >= baoShiOpenCount && baoShiOpenCount != 0)
			{
				//Alert.show("宝石背包已满");
				return result = true;
			}
			if(taskCount >= taskOpenCount && taskOpenCount != 0)
			{
				//Alert.show("任务背包已满");
				return result = true;
			}
			return result;
		}
		
		/**
		 * 根据item id获取item xml
		 * @param itemID
		 * @param callBack
		 * 
		 */		
		public function getItemXML(itemID:uint, callBack:Function, args:Object):void
		{
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + 'item/xml/itemType_' + itemID + '.xml', callBack, args);
		}
		
		/**
		 * 通过品质获得URL
		 * @param quality 品质
		 * @param itemID  图标ID
		 * @return        最终URL
		 * 
		 */		
		public function byQualityGetURL(quality:uint,iconID:uint):String
		{
			var url:String = "";
			switch(quality)
			{
				case Enums.QUALITY_GOOD:
					url = "item/icon/item_" + iconID + "_1.png";
					break;
				case Enums.QUALITY_EXCELLENT:
					url = "item/icon/item_" + iconID + "_2.png";
					break;
				case Enums.QUALITY_FAMOUS:
					url = "item/icon/item_" + iconID + "_3.png";
					break;
				case Enums.QUALITY_RARE:
					url = "item/icon/item_" + iconID + "_4.png";
					break;
				case Enums.QUALITY_NORMAL:
				case Enums.QUALITY_LEGEND:
					url = "item/icon/item_" + iconID + ".png";
					break;
				default :
					url = "item/icon/item_" + iconID + ".png";
					break;
			}
			return url;
		}
		
		/**
		 * 根据道具id取得道具vo 
		 * @param id
		 * @return 
		 * 
		 */
		public function getItemVoByID(id:uint):BaseItemVo
		{
			for each(var o:BaseItemVo in daojuList)
			{
				if (o.itemId == id)
					return o;
			}
			
			return null;
		}
		
		
		/**
		 * 根据道具id取得装备vo 
		 * @param id
		 * @return 
		 * 
		 */
		public function getEquipmentVoByID(id:uint):EquipmentVo
		{
			for each(var o:EquipmentVo in equipmentList)
			{
				if (o.itemId == id)
					return o;
			}
			
			return null;
		}

	}
}